var twoPoint = {
  x1: 0,
  y1: 0,
  x2: 0,
  y2: 0
}
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    seatings: Array,
    left_seatings: Array,
    right_seatings: Array,
    hallNumber: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    navbar: ['三层南', '三层北'],
    currentTab: 0,
    b_selectedIndex: [],
    l_selectedIndex: [],
    r_selectedIndex: [],
    info: "",
    stv: {
      offsetX: 0,                                   //左上角坐标x
      offsetY: 0,                                   //左上角坐标y
      zoom: false,                                  //是否缩放状态
      distance: 0,                                  //两指距离
      scale: 1,                                     //缩放倍数
      centerX: "50%",                                //缩放中心
      centerY: "50%",                                //缩放中心
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    selected(e) {
      console.log('selected');
      let index = e.currentTarget.dataset.index;
      let tag = e.currentTarget.dataset.tag;
      let area = ""
      let row = ""
      let column = ""
      switch (tag) {
        case 'l':
          area = "南区"
          row = Math.floor(index / 4) + 1
          column = index % 4 + 1
          if (this.data.l_selectedIndex[0] == index) {
            // 如果点击已选中的
            let selectedIndex = []
            this.setData({
              l_selectedIndex: selectedIndex
            })
          } else {
            let selectedIndex = [index]
            this.setData({
              l_selectedIndex: selectedIndex,
              b_selectedIndex: [],
              r_selectedIndex: []
            })
          }
          break;
        case 'r':
          area = "北区"
          row = Math.floor(index / 4) + 1
          column = index % 4 + 1
          if (this.data.r_selectedIndex[0] == index) {
            // 如果点击已选中的
            let selectedIndex = []
            this.setData({
              r_selectedIndex: selectedIndex
            })
          } else {
            let selectedIndex = [index]
            this.setData({
              l_selectedIndex: [],
              b_selectedIndex: [],
              r_selectedIndex: selectedIndex
            })
          }
          break;
        case 'b':
          area = "东区"
          row = Math.floor(index / 15) + 1
          column = index % 15 + 1
          if (this.data.b_selectedIndex[0] == index) {
            // 如果点击已选中的
            let selectedIndex = []
            this.setData({
              b_selectedIndex: selectedIndex
            })
          } else {
            let selectedIndex = [index]
            this.setData({
              l_selectedIndex: [],
              b_selectedIndex: selectedIndex,
              r_selectedIndex: []
            })
            break;
          }

      }
      let info = area + "第" + row + "排  " + "第" + column + "列"
      this.setData({
        info: info
      })
    },
    //切换bar
    navbarTap: function (e) {
      this.setData({
        currentTab: e.currentTarget.dataset.idx
      })
      //全局变量
      // app.globalData.currentTab = e.currentTarget.dataset.idx;
    },
    scaled(e) {
      this.setData({
        scaleHeight: ' 1000px',
        scaleWidth: ' 1000px'
      })
    },
    clicked(e) {
      console.log('click');
      this.setData({
        scale: 2
      })
      // this.setData({
      //   scaleHeight: ' 2000px',
      //   scaleWidth: ' 1000px'
      // })
    },
    //事件处理函数
    touchstartCallback: function (e) {
      if (e.touches.length === 1) {
        let { clientX, clientY } = e.touches[0];
        this.startX = clientX;
        this.startY = clientY;
        this.touchStartEvent = e.touches;
      } else {
        let xMove = e.touches[1].clientX - e.touches[0].clientX;
        let yMove = e.touches[1].clientY - e.touches[0].clientY;
        let distance = Math.sqrt(xMove * xMove + yMove * yMove);
        twoPoint.x1 = e.touches[0].pageX * 2
        twoPoint.y1 = e.touches[0].pageY * 2
        twoPoint.x2 = e.touches[1].pageX * 2
        twoPoint.y2 = e.touches[1].pageY * 2
        this.setData({
          'stv.distance': distance,
          'stv.zoom': true, //缩放状态
        })
      }
    },
    //图片手势动态缩放
    touchmoveCallback: function (e) {
      let _this = this
      fn(_this, e)
    },
    touchendCallback: function (e) {
      //触摸结束
      if (e.touches.length === 0) {
        this.setData({
          'stv.zoom': false, //重置缩放状态
        })
      }
    }
  }
})
/**
* fn:延时调用函数
* delay:延迟多长时间
* mustRun:至少多长时间触发一次
*/
var throttle = function (fn, delay, mustRun) {
  var timer = null,
    previous = null;

  return function () {
    var now = +new Date(),
      context = this,
      args = arguments;
    if (!previous) previous = now;
    var remaining = now - previous;
    if (mustRun && remaining >= mustRun) {
      fn.apply(context, args);
      previous = now;
    } else {
      clearTimeout(timer);
      timer = setTimeout(function () {
        fn.apply(context, args);
      }, delay);

    }
  }
}

var touchMove = function (_this, e) {
  //触摸移动中
  if (e.touches.length === 1) {
    //单指移动
    if (_this.data.stv.zoom) {
      //缩放状态，不处理单指
      return;
    }
    let { clientX, clientY } = e.touches[0];
    let offsetX = clientX - _this.startX;
    let offsetY = clientY - _this.startY;
    _this.startX = clientX;
    _this.startY = clientY;
    let { stv } = _this.data;
    stv.offsetX += offsetX;
    stv.offsetY += offsetY;
    let threshold = 100
    stv.offsetX = (stv.offsetX > threshold) ? threshold : stv.offsetX
    stv.offsetX = (stv.offsetX < -threshold) ? -threshold : stv.offsetX
    stv.offsetY = (stv.offsetY > threshold) ? threshold : stv.offsetY
    stv.offsetY = (stv.offsetY < -threshold) ? -threshold : stv.offsetY
    _this.setData({
      stv: stv
    });

  } else if (e.touches.length === 2) {
    let preTwoPoint = JSON.parse(JSON.stringify(twoPoint))
    twoPoint.x1 = e.touches[0].pageX * 2
    twoPoint.y1 = e.touches[0].pageY * 2
    twoPoint.x2 = e.touches[1].pageX * 2
    // 双指缩放
    let xMove = e.touches[1].clientX - e.touches[0].clientX;
    let yMove = e.touches[1].clientY - e.touches[0].clientY;
    let distance = Math.sqrt(xMove * xMove + yMove * yMove);
    let centerX = (e.touches[1].clientX + e.touches[0].clientX) / 2;
    let centerY = (e.touches[1].clientY + e.touches[0].clientY) / 2;
    let distanceDiff = distance - _this.data.stv.distance;
    let newScale = _this.data.stv.scale + 0.005 * distanceDiff;
    if (newScale < 1 || newScale > 2.5) {
      return;
    }
    _this.setData({
      'stv.distance': distance,
      'stv.scale': newScale,
      'stv.centerX': centerX,
      'stv.centerY': centerY,
    })
  } else {
    return;
  }
}

//为touchMove函数节流
const fn = throttle(touchMove, 50, 50);